package com.offcn.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offcn.dao.TCheckgroupCheckitemMapper;
import com.offcn.dao.TCheckgroupMapper;
import com.offcn.pojo.TCheckgroup;

import com.offcn.pojo.TCheckgroupCheckitem;
import com.offcn.service.TCheckgroupService;
import com.offcn.util.ChineseCharToEn;
import com.offcn.util.PageResult;
import com.offcn.util.QueryPageBean;
import com.offcn.vo.TCheckgroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Service(interfaceClass = TCheckgroupService.class)
public class TCheckgroupServiceImpl implements TCheckgroupService {

    @Autowired
    private TCheckgroupMapper tCheckgroupMapper;

    @Autowired

    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;

    @Override
    public PageResult findPage(QueryPageBean PageBean) {
        Page<TCheckgroup> page = new Page<>(PageBean.getCurrentPage(),PageBean.getPageSize());
        QueryWrapper<TCheckgroup> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(PageBean.getQueryString())){
            wrapper.eq("code",PageBean.getQueryString());
            wrapper.or();
            wrapper.like("name",PageBean.getQueryString());
            wrapper.or();
            wrapper.like("helpCode",PageBean.getQueryString());
        }
        Page<TCheckgroup> checkgroupPage = tCheckgroupMapper.selectPage(page, wrapper);
        return new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());
    }

    //添加功能 添加检查组数据  添加中间表  --  有事务
    @Override
    @Transactional
    public void add(int[] checkitemIds, TCheckgroup tCheckgroup) {

        String name = tCheckgroup.getName();
        String helpCode = ChineseCharToEn.getAllFirstLetter(name);
        tCheckgroup.setHelpCode(helpCode.toUpperCase());

        tCheckgroupMapper.insert(tCheckgroup);

        Integer checkgroupId = tCheckgroup.getId();
        for (int itemId : checkitemIds){
            TCheckgroupCheckitem checkgroupCheckitem = new TCheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(checkgroupId);
            checkgroupCheckitem.setCheckitemId(itemId);
            tCheckgroupCheckitemMapper.insert(checkgroupCheckitem);
        }


    }

    //编辑框中回显数据 -- 包含当前组包括的检查项(id)
    @Override
    public TCheckgroup findById(int id) {

        //检查组
        TCheckgroup tCheckgroup = tCheckgroupMapper.selectById(id);
        //对应检查项 -- ids
        LambdaQueryWrapper<TCheckgroupCheckitem> wrapper = new LambdaQueryWrapper<>();
        //设置条件  checkgroup_id = id
        wrapper.eq(TCheckgroupCheckitem::getCheckgroupId,id);
        //筛选 -- 只需要检查项字段
        wrapper.select(TCheckgroupCheckitem::getCheckitemId);
        //List<Object> == List<Map<String,Object>> == List<Integer>
        List<Integer>itemIds = tCheckgroupCheckitemMapper.selectObjs(wrapper).stream().map(o -> (Integer)o).collect(Collectors.toList());
        TCheckgroupVo vo = new TCheckgroupVo();
        vo.setCheckitemIds(itemIds);
        BeanUtils.copyProperties(tCheckgroup,vo);
        return vo;
    }

    //修改 -- 修改检查组和所包含的检查项(中间表修改)
    @Override
    @Transactional
    public void update(int[] checkitemIds, TCheckgroup checkgroup) {
        //自动转换编码
        String name = checkgroup.getName();
        String helpCode = ChineseCharToEn.getAllFirstLetter(name);
        checkgroup.setHelpCode(helpCode.toUpperCase());

        //修改组的信息
        tCheckgroupMapper.updateById(checkgroup);
        Integer checkgroupId = checkgroup.getId();

        //先删除 -- 用组ID做条件
        QueryWrapper<TCheckgroupCheckitem> wrapper = new QueryWrapper<>();
        wrapper.eq("checkgroup_id",checkgroupId);
        tCheckgroupCheckitemMapper.delete(wrapper);

        //再添加
        for (int itemId : checkitemIds){
            TCheckgroupCheckitem checkgroupCheckitem = new TCheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(checkgroupId);
            checkgroupCheckitem.setCheckitemId(itemId);
            tCheckgroupCheckitemMapper.insert(checkgroupCheckitem);
        }
    }

    @Override
    @Transactional
    public void delete(int id) {
        //先删除外键，再删除主键
        QueryWrapper<TCheckgroupCheckitem> wrapper = new QueryWrapper<>();
        wrapper.eq("checkgroup_id",id);
        tCheckgroupCheckitemMapper.delete(wrapper);
        //再删除主键表
        tCheckgroupMapper.deleteById(id);
    }


}
